<?php

include '../../init.php';

// 注册信息
if (empty($_POST)){
	show_msg(1, '数据不能为空', 3, 'index.php');
	exit();
}

// 验证验证码
$post_code = getV($_POST, 'register_validate_code');
$loca_code = getV($_SESSION, 'register_validate_code');
if (strtolower($post_code) != strtolower($loca_code)){
	show_msg(1, '校验码不正确', 3, 'index.php');
	exit();
}
unset($_SESSION['register_validate_code']);


// 校对数据
$account   = getV($_POST, 'account');
$password  = getV($_POST, 'password');
$password2 = getV($_POST, 'password2');
$email     = getV($_POST, 'email');
if ($password != $password2 && $password !== NULL) {
	show_msg(1, '密码填写不正确', 3, 'index.php');
	exit();
}

if (empty($account) || empty($email) || empty($password)) {
	show_msg(1, '必填数据不能为空', 3, 'index.php');
	exit();
}


// 检测后台是否有该用户名了
$array = array();
$sql = "SELECT uid FROM `login_tb` WHERE account='{$account}' LIMIT 1";
$result = my_mysql_query($sql, $array);
if ($result === false) {
	show_msg(1, '系统错误,请稍后重试...', 30, 'index.php');
	exit();
}

// 检测账户名是否已经注册过了
if (count($array[0])>0) {
	show_msg(1, $account.'--->用户名已被注册过了...换个试试', 3, 'index.php');
	exit();
}

// 检测邮箱
$sql = "SELECT uid FROM `userinfo_tb` WHERE email='{$email}' LIMIT 1;";
$result = my_mysql_query($sql, $array);
if (count($array[0])>0) {
	show_msg(1, $account.'--->邮箱已被注册过了...换个试试', 3, 'index.php');
	exit();
}

// 插入信息表
$alias = getV($_POST, 'alias');
$alias = $alias ? $alias : $account;
$sql = "INSERT INTO `userinfo_tb`(email, alias) VALUES('".$email."', '".$alias."');";
if (my_mysql_query($sql, $array, $uid) === false) {
	show_msg(1, '系统错误,请稍后重试...', 30, 'index.php');
	exit();
}

// 插入登录表
$sql = "INSERT INTO `login_tb`(uid, account, password, create_time) VALUES";
$sql .= " ({$uid}, '{$account}', '".md5($password)."',".time().");";
if (my_mysql_query($sql, $array) === false) {
	show_msg(1, '系统错误,请稍后重试...', 30, 'index.php');
	exit();
}


// 处理登录成功.跳转到登录页面
show_msg(0, '注册成功...', 2, '../login/index.php');
